package com.zjuh.qss.biz.ao.impl;

import java.util.Date;
import java.util.List;

import wint.lang.utils.CollectionUtil;

import com.taobao.api.ApiException;
import com.taobao.api.domain.Item;
import com.taobao.api.domain.ItemCat;
import com.taobao.api.domain.TaobaokeItem;
import com.zjuh.qss.biz.ao.TaobaoItemWorkAO;
import com.zjuh.qss.biz.bo.TaobaoOpenBO;
import com.zjuh.qss.biz.bo.TaokeUtil;
import com.zjuh.qss.biz.dao.TaobaoCatDAO;
import com.zjuh.qss.biz.dao.TaobaoItemDAO;
import com.zjuh.qss.biz.dao.TaobaoItemDescriptionDAO;
import com.zjuh.qss.biz.dao.query.TaobaoCatQuery;
import com.zjuh.qss.biz.domain.taobao.TaobaoCatDO;
import com.zjuh.qss.biz.domain.taobao.TaobaoItemDO;
import com.zjuh.qss.biz.domain.taobao.TaobaoItemDescriptionDO;
import com.zjuh.qss.common.JsoupHtmlContentParser;


public class TaobaoItemWorkAOImpl extends BaseAO implements TaobaoItemWorkAO {
	
/*	private TaobaoOpenBO taobaoOpenBO;
	
	private TaobaoCatDAO taobaoCatDAO;
	
	private TaobaoItemDAO taobaoItemDAO;
	
	private TaobaoItemDescriptionDAO taobaoItemDescriptionDAO;
	
	private int maxTaokeIdCount = 19;
	
	private double minCommission = 0.5;
	
	public void runFetchTaobaoItems() {
		try {
			log.warn("fetchTaobaoItems starting... ");
			if (needUpdateCats()) {
				log.warn("fetch taobao cat starting... ");
				int catCount = updateCats(0L);
				log.warn("fetch taobao cat finish. catCount: " + catCount);
			}
			int pageNo = 1;
			long itemCount = 0;
			while (true) {
				TaobaoCatQuery taobaoCatQuery = new TaobaoCatQuery();
				taobaoCatQuery.setPageSize(100);
				taobaoCatQuery.setPageNo(pageNo);
				List<TaobaoCatDO> taobaoCats = taobaoCatDAO.queryForPage(taobaoCatQuery);
				if (CollectionUtil.isEmpty(taobaoCats)) {
					break;
				}
				for (TaobaoCatDO taobaoCatDO : taobaoCats) {
					long cid = taobaoCatDO.getCid();
					List<String> trackIids = taobaoOpenBO.searchTmallSelectedItems(cid);
					itemCount += proccessTrackIids(trackIids, cid);
					log.warn("itemCount: " + itemCount + " for cat:" + taobaoCatDO.getName());
				}
				++pageNo;
				
			}
			log.warn("fetchTaobaoItems finish. itemCount: " + itemCount);
		} catch (Exception e) {
			log.error("fetchTaobaoItems error", e);
		}
	}
	
	private int proccessTrackIids(List<String> trackIids, long cid) throws ApiException {
		int count = 0;
		try {
			if (CollectionUtil.isEmpty(trackIids)) {
				return 0;
			}
			
			List<String> targetTrackIids = CollectionUtil.newArrayList();
			for (String trackId : trackIids) {
				targetTrackIids.add(trackId);
				if (targetTrackIids.size() >= maxTaokeIdCount) {
					
					List<TaobaokeItem> taobaokeItems =  taobaoOpenBO.queryTaobaokeItemsByTrackIids(targetTrackIids);
					List<TaobaoItemDO> taobaoItemDOs = TaokeUtil.toTaoabaoItems(taobaokeItems);
					for (TaobaoItemDO taobaoItemDO : taobaoItemDOs) {
						taobaoItemDO.setCid(cid);
					}
					for (TaobaoItemDO taobaoItemDO : taobaoItemDOs) {
						if (taobaoItemDO.getCommission() < minCommission) {
							continue;
						}
						taobaoItemDAO.insertOrUpdateByItemId(taobaoItemDO);
						++count;
					}
					saveTaobaoItemDescriptions(targetTrackIids);
					
					targetTrackIids = CollectionUtil.newArrayList();
				}
			}
			
		} catch (Exception e) {
			log.error("proccessTrackIids error", e);
		}
		return count;
	}
	
	private void saveTaobaoItemDescriptions(List<String> trackIds) throws ApiException {
		List<Item> items = taobaoOpenBO.queryItemDescsByTrackIids(trackIds);
		if (CollectionUtil.isEmpty(items)) {
			return;
		}
		for (Item item : items) {
			TaobaoItemDescriptionDO taobaoItemDescriptionDO = new TaobaoItemDescriptionDO();
			taobaoItemDescriptionDO.setItemId(item.getNumIid());
			taobaoItemDescriptionDO.setContent(filterContent(item.getDesc()));
			taobaoItemDescriptionDAO.insertOrUpdateByItemId(taobaoItemDescriptionDO);
		}
	}
	
	private String filterContent(String rawData) {
		return JsoupHtmlContentParser.filterTagAndAttributes(rawData);
	}
	
	private boolean needUpdateCats() {
		// 可以设置为1个月更新一次
		TaobaoCatDO taobaoCatDO =  taobaoCatDAO.getLastTaobaoCatDO();
		if (taobaoCatDO == null) {
			return true;
		}
		Date lastModified = taobaoCatDO.getGmtModified();
		long ts = lastModified.getTime();
		long now =(new Date()).getTime();
		long deltaDays = (now - ts)/ 1000 / 3600 / 24;
		log.warn("delta days:" + deltaDays);
		if (deltaDays >= 30) {
			return true;
		}
		
		return false;
	}
	
	private int updateCats(long parentCid) throws ApiException {
		List<ItemCat> itemCats = taobaoOpenBO.getItemCatsByParentId(parentCid);
		if (CollectionUtil.isEmpty(itemCats)) {
			return 0;
		}
		int count = 0;
		for (ItemCat itemCat : itemCats) {
			TaobaoCatDO taobaoCatDO = new TaobaoCatDO();
			taobaoCatDO.setCid(itemCat.getCid());
			taobaoCatDO.setName(itemCat.getName());
			taobaoCatDO.setParentCid(taobaoCatDO.getParentCid());
			taobaoCatDAO.saveOrUpdateByCid(taobaoCatDO);
			++count;
		}
		
		for (ItemCat itemCat : itemCats) {
			if (!itemCat.getIsParent()) {
				continue;
			}
			count += updateCats(itemCat.getCid());
		}
		log.warn("cat count:" + count);
		return count;
	}
	
	public void setTaobaoOpenBO(TaobaoOpenBO taobaoOpenBO) {
		this.taobaoOpenBO = taobaoOpenBO;
	}

	public void setTaobaoCatDAO(TaobaoCatDAO taobaoCatDAO) {
		this.taobaoCatDAO = taobaoCatDAO;
	}

	public void setTaobaoItemDAO(TaobaoItemDAO taobaoItemDAO) {
		this.taobaoItemDAO = taobaoItemDAO;
	}

	public void setTaobaoItemDescriptionDAO(TaobaoItemDescriptionDAO taobaoItemDescriptionDAO) {
		this.taobaoItemDescriptionDAO = taobaoItemDescriptionDAO;
	}
*/
}
